﻿using System;
using Service;
using System.Web.UI.WebControls;
using System.Linq;

namespace ECommerce.Administration.Controls
{
	public partial class LocalStringResourceList : AdminBaseUsercontrol
	{
		private ILocalStringResourceService _LocalStringResourceService;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (SiteContext.Current == null)
			{
				Server.Transfer("~Administration/AdminLogin.aspx");
			}

			if (!IsPostBack)
			{
				ButtonSearch.Text = GetLocalResourceString("Admin.SearchButton.Text");
			}
		}

		protected void ButtonSearch_Click(object sender, EventArgs e)
		{
			BindData(TextSearchKey.Text.Trim());
		}

		protected void GridLocalstringResource_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
		{
			if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.Header)
			{
				e.Row.Cells[1].Text = GetLocalResourceString("Admin.LocalstringResourceList.NameHeader");
				e.Row.Cells[2].Text = GetLocalResourceString("Admin.LocalstringResourceList.ValueHeader");
			}

			if (e.Row.DataItem != null)
			{
				var item = e.Row.DataItem as Data.Entity.LocalStringResource;

				HyperLink HyperLinkCategory = e.Row.FindControl("HyperLinkLSR") as HyperLink;

				HyperLinkCategory.NavigateUrl = "~/Administration/EditLocalStringResource.aspx?localstringresourceid=" + item.LocaleStringResourceID.ToString();
				HyperLinkCategory.Text = item.ResourceName;
			}
		}

		private void BindData(string key)
		{
			if (_LocalStringResourceService == null)
			{
				_LocalStringResourceService = new LocalStringResourceService();
			}
			
			var languageID = 0;

			if (SiteContext.Current.WorkingLanguage != null)
			{
				languageID = SiteContext.Current.WorkingLanguage.LanguageId;
				System.Collections.Generic.IEnumerable<Data.Entity.LocalStringResource> data = _LocalStringResourceService.GetLocaleStringResourceByLanguage(languageID);

				if (!string.IsNullOrEmpty(key))
				{
					data = data.Where(exp => exp.ResourceValue.Contains(key) || exp.ResourceName.ToLower().Contains(key.ToLower()));
				}

				GridLocalstringResource.DataSource = data;
				GridLocalstringResource.DataBind();
			}
		}

		public void DeleteSelectedLocalStringResource()
		{
			if (GridLocalstringResource.Rows.Count > 0)
			{
				if (_LocalStringResourceService == null)
				{
					_LocalStringResourceService = new Service.LocalStringResourceService();
				}

				foreach (System.Web.UI.WebControls.GridViewRow rowItem in GridLocalstringResource.Rows)
				{
					var chBox = rowItem.FindControl("cbLocaleStringResource") as CheckBox;
					var hfLocaleStringResourceId = rowItem.FindControl("hfLocaleStringResourceId") as HiddenField;

					bool isChecked = chBox.Checked;
					int localeStringResourceId = int.Parse(hfLocaleStringResourceId.Value);

					if (isChecked)
					{
						_LocalStringResourceService.DeleteLocaleStringResource(localeStringResourceId);
					}
				}

				BindData(TextSearchKey.Text.Trim());
			}
		}
	}
}